package sonic.game.animation;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 * Animation Set
 * 
 * A group of animations with labels for easy access
 * 
 * @author RGreenlees
 *
 */

public class AnimationSet {

	/** animID and anims ArrayLists are always in sync, allowing retrieval of an Animation object using a search string */
	private ArrayList<String> animID = new ArrayList<String>();
	private ArrayList<Animation> anims = new ArrayList<Animation>();
	
	private Animation currentAnim;
	
	public Animation getCurrentAnimation()
	{
		return currentAnim;
	}
	
	/** Using the supplied search string, set the appropriate animation as the currently active one */
	public void setCurrentAnimation(String newAnim)
	{
		Animation foundAnim = anims.get(animID.indexOf(newAnim));
		if (foundAnim != null && foundAnim != currentAnim)
		{
			currentAnim = anims.get(animID.indexOf(newAnim));
			currentAnim.reset();
		}
	}
	
	/** Add a new animation to the set using the specified identifier and animation */
	public void addAnimation(String identifier, Animation newAnim)
	{
		animID.add(identifier);
		anims.add(animID.indexOf(identifier), newAnim);
	}
	
	/** Removes the identifier and associated animation */
	public void removeAnimation(String identifier)
	{
		int index;
		index = animID.indexOf(identifier);
		
		if (index > -1)
		{
			animID.remove(index);
			anims.remove(index);
		}
	}
	
	/** Hook to expose the current animations play rate */
	public void setCurrentAnimRate(float newRate)
	{
		currentAnim.setAnimRate(newRate);
	}
	
	/** Hook to expose current animation's current frame */
	public BufferedImage getCurrentFrame(boolean rev)
	{
		return currentAnim.getCurrentFrame(rev);
	}
	
	/** Hook to update current Animation */
	public void update(double deltaTime)
	{
		currentAnim.update(deltaTime);
	}
}
